import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css' 
import {store} from '@/common/store'
import { ElMessage } from "element-plus"
const app = createApp(App)
app.use(router)
app.use(ElementPlus, {
    locale: zhCn,
  })
axios.defaults.baseURL='http://localhost:8080'//配置这个将来发请求省略http://localhost:8080
axios.interceptors.request.use(function (config) {
      if(store.logined){//如果登陆了
        config.headers["Authorization"] = store.token 
      } 
    //比如登陆的验证 请求头添加 jwt或者userid  
    console.log("axios 要请求后端了哦")
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });
// 添加响应拦截器
axios.interceptors.response.use(function (response) { 
  if(response.data.code*1==10003){//过期
       console.log('登录过期，清空本地用户信息');
       store.logined = false
       store.token = ''
       store.userinfo ={id:"",username:"",name:"",address:""},
       sessionStorage.removeItem('token')
       sessionStorage.removeItem("userinfo") 
       ElMessage.error('登录过期')
    }else  if(response.data.code*1==10001||response.data.code*1==10002){
       ElMessage.error('没有权限或非法，如果没有登录，请登录后重试')
    }
  console.log("收到后端返回的数据了！"+response.data)  
  return response;
}, function (error) {  
  return Promise.reject(error);
}); 
app.mount('#app')